package com.ckzp.jfinal.system.util;

import com.ckzp.core.json.Jmode;
import com.ckzp.core.util.DateUtil;
import com.ckzp.jfinal.plugins.UpImage;
import com.ckzp.jfinal.system.model.SysNotice;
import com.ckzp.jfinal.system.model.SysNoticeUser;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 51594 on 2020/9/1.
 */
public class SysNoticeUtil {

    public static SysNoticeUtil n() {
        return new SysNoticeUtil();
    }

    /**
     * @param p
     * @param size
     * @param u_id
     * @return
     */
    public List getUserNoticeJsonList(int p, int size, int u_id) {
        String sql = "from sys_notice where pub_tag=1 order by sindex desc";
        Page<SysNotice> pages = SysNotice.dao.paginate(p, size, "select * ", sql, u_id);
        List list = pages.getList();
        List jsonlist = new ArrayList();
        String readstr = getUserReadNotice(u_id);
        for (int k = 0; k < list.size(); k++) {
            SysNotice sn = (SysNotice) list.get(k);
            Jmode jm = Jmode.n();
            jm.set("not_id", sn.mod.getInt("not_id"));
            jm.set("title", sn.mod.getStr("not_title"));
            jm.set("times", DateUtil.getDateHF(sn.mod.getDate("not_time")));
            boolean read = readstr.indexOf("|" + (sn.mod.getStr("not_id")) + "|") > -1;
            jm.set("read", read);

            jsonlist.add(jm);
        }
        return jsonlist;
    }

    /**
     * @param u_id
     * @return
     */
    private String getUserReadNotice(int u_id) {
        StringBuffer sb = new StringBuffer("|");
        String sql = "select * from sys_notice_user where m_id=?";
        List list = Db.query(sql, u_id);
        if (list != null) {
            for (int k = 0; k < list.size(); k++) {
                Record rec = (Record) list.get(k);
                sb.append(rec.getStr("not_id") + "|");
            }
        }
        return sb.toString();
    }


    public void saveUserNoticeLog(int u_id, int not_id) {
        SysNoticeUser nu = SysNoticeUser.dao.findByIds(not_id, u_id);
        if (nu == null) {
            nu = new SysNoticeUser().set("m_id", u_id).set("not_id", not_id).set("times", 1).set("last_time", new Date());
            nu.save();
        } else {
            nu.set("times", nu.mod.getInt("times") + 1).set("last_time", new Date()).update();
        }
    }

    public Jmode getNoticeInfo(int not_id, int u_id) {
        saveUserNoticeLog(u_id, not_id);
        Jmode jmode = Jmode.n().set("not_id", not_id);
        SysNotice sn = SysNotice.dao.findById(not_id);
        if (sn != null) {
            jmode.set("title", sn.mod.getStr("not_title"));
            jmode.set("text", sn.mod.getStr("not_text"));
            jmode = formatNoticeImg(jmode, sn, "data_img1");
            jmode = formatNoticeImg(jmode, sn, "data_img2");
            jmode = formatNoticeImg(jmode, sn, "data_img3");
            jmode = formatNoticeImg(jmode, sn, "data_img4");
            jmode = formatNoticeImg(jmode, sn, "data_img5");
        } else {
            jmode.set("text", "信息不存在，或已删除[" + not_id + "]");
        }
        return jmode;
    }

    /**
     * @param jmode
     * @param sn
     * @param columns
     * @return
     */
    private Jmode formatNoticeImg(Jmode jmode, SysNotice sn, String columns) {
        String img = sn.mod.getStr(columns);
        if (img != null && img.length() > 0) {
            jmode.set(columns, UpImage.TransUrl(img));
            jmode.set("has_" + columns, true);
        } else {
            jmode.set("has_" + columns, false);
            jmode.set(columns, "");
        }
        return jmode;
    }
}
